package com.easonji.attendance.controller;



import com.easonji.common.base.controller.BaseController;

import com.easonji.core.annotion.Permission;
import com.easonji.core.log.LogObjectHolder;
import com.easonji.common.base.query.BaseQuery;
import com.easonji.common.base.response.Response;
import com.easonji.common.base.response.enums.ResponseEnum;
import com.easonji.common.util.StringUtils;

import com.easonji.attendance.service.IRecordStateService;
import com.easonji.attendance.model.RecordState;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.BindingResult;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * <p>
 * 补卡状态 前端控制器
 * </p>
 *
 * @author easonji
 * @since 2018-10-05
 */
@Controller
@RequestMapping("/attendance/recordState")
public class RecordStateController extends BaseController {


    /**
    * 补卡状态页面前缀
    */
    private final static String PREFIX = "attendance/recordState/";

    @Resource
    private IRecordStateService recordStateService;

    /**
    * 跳转到补卡状态首页
    */
    @Permission
    @GetMapping("")
    public String index() {
        return PREFIX + "recordState";
    }

    /**
    * 查询补卡状态列表
    */
    @ResponseBody
    @PostMapping("/list")
    public Object queryList(BaseQuery baseQuery) {
        Object result = recordStateService.queryList(baseQuery);
        return result;
    }

    /**
    * 跳转到添加补卡状态页面
    */
    @Permission
    @GetMapping("/to_add")
    public String toAdd(Model model) {
        return PREFIX + "recordState_edit";
    }

    /**
    * 跳转到修改补卡状态页面
    */
    @Permission
    @GetMapping("/to_update/{resId}")
    public String toUpdate(@PathVariable Integer resId, Model model) {
        Object result = recordStateService.queryRowById(resId);
        LogObjectHolder.me().set(result);
        model.addAttribute("recordState", result);
        return PREFIX + "recordState_edit";
    }

    /**
    * 新增补卡状态
    */
    @ResponseBody
    @Permission
    @PostMapping("/save")
    public Response add(@Valid RecordState recordState, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return failure(bindingResult);
        }
        int result = recordStateService.save(recordState);
        if (result == 0) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.SAVE_SUCCESS);
    }

    /**
    * 修改补卡状态
    */
    @ResponseBody
    @Permission
    @PostMapping("/update")
    public Response update(@Valid RecordState recordState, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return failure(bindingResult);
        }
        int result = recordStateService.update(recordState);
        if (result == 0) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
    * 删除补卡状态
    */
    @ResponseBody
    @Permission
    @PostMapping("/remove")
    public Response remove(@RequestParam Integer resId) {
        if (StringUtils.isEmpty(resId)) {
            return failure(ResponseEnum.PARAMETER_ERROR);
        }
        int result = recordStateService.delete(resId);
        if (result == 0) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.REMOVE_SUCCESS);
    }

}
